#include "../include/pathStack.h"


int init_stack(commandStack_t *stack){
    if(stack == NULL){ 
        return -1; 
    }
    memset(stack, 0, sizeof(commandStack_t));
    stack->top = -1;
    for(int i = 0; i < 100; i++){
        stack->num[i] = -1;
    }
    char *p = (char *)calloc(1, 100);
    stack->username = p;
    return 0;
}


int push_stack(commandStack_t *stack, int id){
    if(stack->size > 100){
        return -1;
    }
    else{
        stack->top++;
        stack->num[stack->top] = id;
        stack->size++;

    }
    return 0;
}


int pop_stack(commandStack_t *stack){
    if(stack->size == 0){
        printf("stack is empty! pop stack failed!\n");
        return -1;
    }
    else{
        stack->num[stack->top] = -1;
        stack->top--;
        stack->size--;
    }
    return 0;
}

int clear_stack(commandStack_t *stack){
    if(stack == NULL){
        printf("stack is NULL! clear failed!\n");
        return -1;
    }
    else{
    memset(stack->num, -1, sizeof(stack->num));
    stack->top = -1;
    stack->bottom = -1;
    stack->size = 0;
    }
    return 0;
}

int print_stack(commandStack_t *stack){
    for(int i = 0; i < stack->size; i++){
        printf("%3d", stack->num[i]);
    }
    printf("\n");
    return 0;
}

int copy_stack(commandStack_t *destStack, commandStack_t *srcStack){
    clear_stack(destStack);
    for(int i = 0; i < srcStack->size && destStack->size < 100; i++){
        destStack->num[i] = srcStack->num[i];
        destStack->top++;
        destStack->size++;
    }
    if(destStack->size >= 100){
        printf("destStack is full !copy failed!\n");
        return -1;
    }
    return 0;
}
int reverse_stack(commandStack_t *destStack, commandStack_t *srcStack){
    clear_stack(destStack);
    int temp;
    while(srcStack->size > 0)
    {
        temp = srcStack->num[srcStack->top];
        pop_stack(srcStack);
        push_stack(destStack,temp);
    }
    if(destStack->size >= 100){
        printf("destStack is full !copy failed!\n");
        return -1;
    }
    return 0;
}


/* int main(int argc, char *argv[]){ */

/*     commandStack_t stack; */
/*     commandStack_t stack2; */
/*     init_stack(&stack); */
/*     init_stack(&stack2); */
/*     push_stack(&stack, 10); */
/*     push_stack(&stack, 11); */
/*     push_stack(&stack, 12); */
/*     push_stack(&stack, 13); */
/*     push_stack(&stack, 14); */
/*     print_stack(&stack); */
/*     push_stack(&stack2, 21); */
/*     push_stack(&stack2, 22); */
/*     print_stack(&stack2); */
/*     copy_stack(&stack, &stack2); */
/*     print_stack(&stack); */
/*     print_stack(&stack2); */
    
/*     return 0; */
/* } */
